<template>
  <div class="family">
    <div class="function-container-back" v-on:click="backToPreviousPage">
      <img src="../../assets/images/house/return.png" alt="Return Icon" class="back-icon">
    </div>
    <div class="family-title">
      <h1>家庭信息修改</h1>
    </div>
    <el-row gutter="20" class="family-hand">
      <el-form :label-position="labelPosition" label-width="80px" :model="familyFormData">
        <el-col :span="8">
          <el-form-item label="家庭名称">
            <el-input v-model="familyFormData.familyName"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-button icon="el-icon-search" circle v-on:click="getTableData"></el-button>
          <el-button type="warning" icon="el-icon-star-off" circle  v-on:click="reset"></el-button>
          <el-button type="primary" icon="el-icon-edit" circle v-on:click="add"></el-button>
        </el-col>
      </el-form>

    </el-row>

    <div class="family-table">
      <el-table
          :data="tableData"
          style="width: 1400px"
          height="700px"
          border>
        <el-table-column
            prop="familyName"
            label="家庭名称"
            >
        </el-table-column>
        <el-table-column
            prop="familyMessage"
            label="家庭信息"
            >
        </el-table-column>
        <el-table-column
            prop="familyLocation"
            label="家庭地址"

        >
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
      <el-form ref="dialogDataForm" :model="dialogDataForm" label-width="150px" size="small" style="padding-right: 40px;">
        <el-form-item label="家庭名称">
          <el-input v-model="dialogDataForm.familyName" />
        </el-form-item>
        <el-form-item label="家庭信息">
          <el-input v-model="dialogDataForm.familyMessage" />
        </el-form-item>
        <el-form-item label="家庭地址">
          <el-input v-model="dialogDataForm.familyLocation" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
          <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate" size="small">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>

import api from "@/api/family";

export default {
  name: "family.vue",
  data(){
    return{
      dialogVisible:false,
      dialogDataForm:{},

      familyFormData:{

      },
      tableData: [],

      homeOptions:[

      ]
    }
  },
  mounted() {
    this.getTableData()
    this.getFamily()
  },
  methods:{
    reset(){
      this.familyFormData={}
      this.getTableData()
    },
    getTableData(){
      api.getPageList(1,100,this.familyFormData).then((response)=>{
        if (response.code===200){
          console.log("eq",response)
          this.tableData=response.data.records
        }else {
          this.$message.error(response.message)
        }
      })
    },
    handleEdit(index,row){
      this.dialogVisible=true
      // console.log("index",index)
      // console.log("row",row)
      this.dialogDataForm=row
    },
    handleDelete(index,row){
      console.log("row",row)
      api.removeById(row.deviceId).then((re)=>{
        if (re.code===200){
          this.reset()
          this.$message.success(re.message)
        }else {
          this.$message.error(re.message)
        }
      })
    },
    backToPreviousPage() {
      this.$router.go(-1); // 返回上一页
    },
    saveOrUpdate(){
      this.dialogDataForm.familyId=1
      console.log("this.dialogDataForm",this.dialogDataForm)
      api.save(this.dialogDataForm).then((response)=>{
        if (response.code===200){
          this.dialogDataForm={}
          this.reset()
        }
        this.dialogVisible=false
      })

    },
    add(){
      this.dialogVisible=true
      this.dialogDataForm={}
      this.getFamily()
    },

  }
}
</script>

<style scoped>
.family{

}

.family-hand{
  padding-top: 80px;
  padding-left: 275px;

/deep/ .el-form-item__label{
  color: #ffffff;
}

/deep/ .el-input__inner{
  width: 410px;
}
}

.family-table{
  margin: 0 auto;
  padding-left: 275px;
}

.family-container-back{
  position: absolute;
  top: 10px;
  left: 10px;
  display: flex;
  font-size: 18px; /* Slightly larger font size */
  color: white;
  cursor: pointer;
  height: 28px;
}

.function-container-back{
  position: absolute;
  top: 10px;
  left: 10px;
  display: flex;
  font-size: 18px; /* Slightly larger font size */
  color: white;
  cursor: pointer;
}

.family-title{
  position: absolute;
  top: 10px;
  left: 900px;
  color: white;
  height: 25px;
}
</style>
